// pages/mes/edit-home/edit-home.js
const app = getApp();
import Toast from '../../../component/toast/toast';
const uploadFile = require('../../../lib/aliyun/uploadAliyun.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        coverImg: '',
        defaultCls: 'weui-uploader__input-box',
        introduce: '',
        content: '',
        label:'',
        defaultCover:'https://hear2017.oss-cn-shanghai.aliyuncs.com/xianjian_wx_image/home1.png'
    },
    chooseImg() {
        let that = this;
        wx.chooseImage({
            sizeType: ['compressed'],
            count: 1,
            success(res) {
                console.log(res.tempFilePaths[0]);
                that.setData({
                    coverImg: res.tempFilePaths[0]
                });
                that.setData({
                    defaultCls: 'cover-img'
                });
            }
        })
    },
    inputIntroduce(e) {
        this.setData({
            introduce: e.detail.value
        });
    },
    inputLabel(e) {
        this.setData({
            label: e.detail.value
        });
    },
    commitEdit() {
        let that = this;
        let introduce = that.data.introduce;
        let label = that.data.label;
        if(label.length < 2){
            Toast("请填写职业经历哦");
            return;
        }
        if (introduce.length < 50) {
            Toast("自我介绍不能少于50字呦");
            return;
        }
        // if (!that.data.coverImg) {
        //     Toast("请上传封面照呦");
        //     return;
        // }
        wx.showLoading({
            title: '信息上传中...',
        });
        console.log("-------->"+that.data.coverImg);
        if (that.data.coverImg && that.data.defaultCover != that.data.coverImg) {
            let dir = 'xianjian_wx_image';
            that.uploadSyncImg(that.data.coverImg, dir).then(res => {
                if (res.statusCode == 200) {
                    let imgUrl = res.imgUrl;
                    console.log(imgUrl);
                    that.saveUserInfo({
                        introduce: introduce,
                        imgUrl: imgUrl,
                        label:label
                    });

                } else {
                    wx.hideLoading();
                    console.log("success:" + JSON.stringify(res));
                    Toast(res.data.Message);
                }
            }, err => {
                wx.hideLoading();
                console.log("err:" + JSON.stringify(err));
                Toast("图片上传失败！")
            });
        } else {
            that.saveUserInfo({
                introduce: introduce,
                label: label,
                imgUrl: this.data.defaultCover
            });
        }

    },
    uploadSyncImg(path, dir) {
        let promis = new Promise(function(resolve, reject) {
            uploadFile({
                filePath: path,
                dir: dir,
                success: function(res) {
                    resolve(res);
                },
                fail: function(res) {
                    reject(res);
                }
            });
        });
        return promis;
    },
    saveUserInfo(parmas) {
        wx.request({
            url: app.globalData.serverAddress.saveUserInfo,
            data: {
                UserInfoId: app.userInfo.Id,
                Introduction: parmas.introduce,
                CoverPhoto: parmas.imgUrl,
                Label:parmas.label,
                WorksCover: ''
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log("success：" + JSON.stringify(res));
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    wx.navigateBack({
                        delta: 1,
                    })
                }
                Toast(res.data.Message);
            },
            fail: function(res) {
                console.log(JSON.stringify(res));
            },
            complete: function(res) {
                wx.hideLoading();
            },
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        if (options.parmas){
            let obj = JSON.parse(options.parmas);
            this.setData({
                introduce: obj.content,
                label: obj.label,
                coverImg: obj.coverPhoto,
                defaultCover: obj.coverPhoto
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})